/**
 * @title: SentinelConfig
 * @projectName mscloud
 * @Description TODO
 * @Author
 * @Date 2022/2/16 14:46
 */
package com.atguigu.springcloud.cloudsentinelgatewaydevelop.config;


import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.WebFluxCallbackManager;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;

import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.sentinel.datasource.*;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.sentinel.DemoUrlBlockHandler;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.sentinel.MyRequestOriginParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class SentinelConfig {
    @Autowired
    AuthorityRuleMysqlDataSource authorityRuleMysqlDataSource;
    @Autowired
    SystemRuleMysqlDataSource systemRuleMysqlDataSource;
    @Autowired
    DegradeRuleMysqlDataSource degradeRuleMysqlDataSource;
    @Autowired
    FlowRuleMysqlDataSource flowRuleMysqlDataSource;
    @Autowired
    ParamFlowRuleMysqlDataSource paramFlowRuleMysqlDataSource;

    @PostConstruct
    public void init() {
        WebFluxCallbackManager.setBlockHandler(new DemoUrlBlockHandler());
        WebFluxCallbackManager.setRequestOriginParser(new MyRequestOriginParser());
        //这些自动获取功能需要替换为非阻塞形式
        AuthorityRuleManager.register2Property(authorityRuleMysqlDataSource.getProperty());
        SystemRuleManager.register2Property(systemRuleMysqlDataSource.getProperty());
        DegradeRuleManager.register2Property(degradeRuleMysqlDataSource.getProperty());
        ParamFlowRuleManager.register2Property(paramFlowRuleMysqlDataSource.getProperty());
        FlowRuleManager.register2Property(flowRuleMysqlDataSource.getProperty());
        //使用mysql存储目的是为与nacos解耦
        //1.建立个响应式的定时线程
        //2.更新loads的方式的方程式如下:
        //flowRuleSource.getFlowRules().map(JsonString->FlowRule实例).collectList().subscribe(FlowRuleManager::loadRule)

    }
}
